using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors.Layout;
using System.Windows.Media;
using System.Windows.Documents;

namespace WPFTemplateLib.WpfHelpers
{
	/// <summary>
	/// WPF 窗口帮助类
	/// </summary>
	public static class WpfWindowHelper
	{
		/// <summary>
		/// [dlgcy] 获取激活的窗口
		/// </summary>
		/// <param name="isFallbackToMainWindowWhenNoActive">没有激活窗口时是否回落为主窗口（默认为 false）</param>
		/// <returns>激活的窗口，没有则为null</returns>
		public static Window GetActiveWindow(bool isFallbackToMainWindowWhenNoActive = false)
		{
			Window activeWindow = Application.Current?.Windows.OfType<Window>().FirstOrDefault(w => w.IsActive);
			if (activeWindow == null && isFallbackToMainWindowWhenNoActive)
			{
				activeWindow = Application.Current?.MainWindow;
			}

			return activeWindow;
		}

		#region 蒙板弹窗

		/// <summary>
		/// 弹窗时带蒙板
		/// </summary>
		/// <param name="win">弹窗</param>
		/// <param name="maskColorStr">蒙板颜色字符串</param>
		public static void ShowWithMask(this Window win, string maskColorStr = "#88000000")
		{
			AddMask(win, maskColorStr);
			win.Show();
		}

		/// <summary>
		/// 模态弹窗时带蒙板
		/// </summary>
		/// <param name="win">弹窗</param>
		/// <param name="maskColorStr">蒙板颜色字符串</param>
		/// <returns>模态弹窗结果</returns>
		public static bool? ShowDialogWithMask(this Window win, string maskColorStr = "#88000000")
		{
			AddMask(win, maskColorStr);
			win.SetOwnerActiveWindow();
			return win.ShowDialog();
		}

		/// <summary>
		/// 给弹窗后面加上蒙板
		/// </summary>
		/// <param name="showUpWin">要弹出的弹窗</param>
		/// <param name="maskColorStr">蒙板颜色字符串</param>
		public static void AddMask(this Window showUpWin, string maskColorStr = "#88000000")
		{
			Visual parent = GetActiveWindow(true);
			if(parent == null)
				return;

			var layer = AdornerLayer.GetAdornerLayer(parent);
			if(layer == null)
				return;

			var adornerContainer = new AdornerContainer(layer)
			{
				Child = new Border() { Background = new SolidColorBrush(MediaColorHelper.ColorStrToMediaColor(maskColorStr)) }
			};
			layer.Add(adornerContainer);

			if(showUpWin != null)
			{
				showUpWin.Closed += delegate
				{
					layer.Remove(adornerContainer);
				};
			}
		}

		#endregion

		#region 设置 Owner

		/// <summary>
		/// 设置窗口的 Owner 为指定的窗口
		/// </summary>
		public static void SetOwner(this Window win, Window owner)
		{
			win.Owner = owner;
		}

		/// <summary>
		/// 设置窗口的 Owner 为主窗口
		/// </summary>
		public static void SetOwnerMainWindow(this Window win)
		{
			win.Owner = Application.Current?.MainWindow;
		}

		/// <summary>
		/// 设置窗口的 Owner 为激活的窗口
		/// </summary>
		/// <param name="win">窗口</param>
		/// <param name="isFallbackToMainWindowWhenNoActive">没有激活窗口时是否回落为主窗口（默认为 true）</param>
		public static void SetOwnerActiveWindow(this Window win, bool isFallbackToMainWindowWhenNoActive = true)
		{
			win.Owner = GetActiveWindow(isFallbackToMainWindowWhenNoActive);
		}

		#endregion

		#region ShowDialog 扩展

		/// <summary>
		/// 弹出模态窗口（设置 Owner）
		/// </summary>
		/// <param name="win">弹出的窗口</param>
		/// <param name="owner">拥有者</param>
		/// <returns></returns>
		public static bool? ShowDialog(this Window win, Window owner)
		{
			win.SetOwner(owner);
			return win.ShowDialog();
		}

		/// <summary>
		/// 弹出模态窗口（设置 Owner 为主窗口）
		/// </summary>
		/// <param name="win">弹出的窗口</param>
		/// <returns></returns>
		public static bool? ShowDialogForMainWindow(this Window win)
		{
			win.SetOwnerMainWindow();
			return win.ShowDialog();
		}

		/// <summary>
		/// 弹出模态窗口（设置 Owner 为活跃窗口）
		/// </summary>
		/// <param name="win">弹出的窗口</param>
		/// <param name="isFallbackToMainWindowWhenNoActive">没有激活窗口时是否回落为主窗口（默认为 true）</param>
		/// <returns></returns>
		public static bool? ShowDialogForActiveWindow(this Window win, bool isFallbackToMainWindowWhenNoActive = true)
		{
			win.SetOwnerActiveWindow(isFallbackToMainWindowWhenNoActive);
			return win.ShowDialog();
		}

		#endregion
	}
}
